package liming.tool.handle.net.udp2.socket;

import liming.tool.handle.ReceiveMap;
import liming.tool.handle.net.root.NetReceiveMap;
import liming.tool.handle.net.root.base.NetType;

import java.net.InetAddress;

public class UdpReceiveMap extends NetReceiveMap {
    private transient int length = 0;//分片时的大小

    public UdpReceiveMap() {
        super();
        netType = NetType.UDP;
    }

    public UdpReceiveMap(ReceiveMap receiveMap) {
        super(receiveMap);
        netType = NetType.UDP;
    }

    public int getLength() {
        return length;
    }

    public UdpReceiveMap setLength(int length) {
        this.length = length;
        return this;
    }

    @Override
    public UdpReceiveMap setIP(InetAddress inetAddress, int port) {
        super.setIP(inetAddress, port);
        return this;
    }

    public UdpReceiveMap copy() {
        return new UdpReceiveMap(this);
    }

    @Override
    public UdpReceiveMap setByte(int x, byte value) {
        if (x == 0) bytes[0] = (byte) ((bytes[0] & 0xC0) | (value & 0x3f));
        else super.setByte(x, value);
        return this;
    }

    @Override
    public UdpReceiveMap setBit(int x, int y, boolean value) {
        if (x == 0 && (y >= 0 && y <= 1))
            throw new RuntimeException("Illegal assignment at (0, 0) or (0, 1)");
        super.setBit(x, y, value);
        return this;
    }

    public boolean isResAll() {
        return getBit(0, 0);
    }

    public UdpReceiveMap setResAll(boolean value) {
        bytes[0] = (byte) ((int) bytes[0] & 0xbf | (value ? 1 : 0) << 7);
        return this;
    }

    public boolean isRm() {
        return getBit(0, 1);
    }

    public UdpReceiveMap setRm(boolean value) {
        bytes[0] = (byte) ((int) bytes[0] & 0x7f | (value ? 1 : 0) << 6);
        return this;
    }

    @Override
    public String toString() {
        return "UdpReceiveMap{" +
                "length=" + length +
                "," + super.toString() +
                '}';
    }
}
